package com.neuedu.hcp.controller;

import com.neuedu.hcp.entity.RespBean;
import com.neuedu.hcp.entity.Staff;
import com.neuedu.hcp.entity.StaffLoginParam;
import com.neuedu.hcp.service.IStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * 登录操作
 * @author ZZD
 * @date 2021/4/27 16:08
 */
@RestController
public class LoginController {
    @Autowired
    private IStaffService staffService;

    /**
     * 登录之后返回token
     * @param staffLoginParam
     * @param request
     * @return  http://127.0.0.1:8081/hcp/login
     */
    @PostMapping("/login")
    public RespBean login(@RequestBody StaffLoginParam staffLoginParam,HttpServletRequest request){
        return staffService.login(staffLoginParam.getUsername(),staffLoginParam.getPassword(),staffLoginParam.getCode(),request);
    }

    /**
     * 通过员工id获取员工信息
     * @param principal
     * @return
     */
    @GetMapping("/staff/info")
    public Staff getStaffInfo(Principal principal){
        if(null==principal){
            return null;
        }
        String staffId = principal.getName();
        Staff staff =staffService.getStaffByUsername(staffId);
        return staff;
    }

    /**
     * 退出登录
     * @return
     */
    @PostMapping("/logout")
    public RespBean logout(){
        return RespBean.success("注销成功");
    }


}
